package com.tiancheng.trade.payment.channel.umsdhjt.settle;

import com.tiancheng.trade.commom.core.exception.Asserts;
import com.tiancheng.trade.commom.core.utils.IdWorkerUtils;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.commom.web.http.PlatformRequest;
import com.tiancheng.trade.commom.core.utils.DateTimeUtil;
import com.tiancheng.trade.payment.channel.ums.utils.SecureUtil;
import com.tiancheng.trade.payment.channel.umsdhjt.UmsDhjtProperties;
import com.tiancheng.trade.payment.channel.umsdhjt.reponse.UmsDhjtSettleQueryBalancRes;
import com.tiancheng.trade.payment.channel.umsdhjt.reponse.UmsDhjtSettleResponse;
import com.tiancheng.trade.payment.channel.umsdhjt.request.*;
import org.apache.http.client.methods.HttpPost;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author: kellen
 * @Description: 银商大华捷通转账划付接口
 * @Date: create in 2024/12/5 20:01
 */
@Component
public class UmsDhjtSettleClient {

    @Resource
    private PlatformRequest platformRequest;

    /**
     * 发起转账划付请求
     * transType=withdrawals
     *
     * @param umsDhjtProperties 配置
     * @param settleBaseRequest 转账划付参数
     * @return
     */
    public UmsDhjtSettleResponse upload(UmsDhjtProperties umsDhjtProperties, UmsSettleWithdrawalsRequest settleBaseRequest) {
        final UmsSettleRequestHead head = new UmsSettleRequestHead();
        head.setOrgSenderID(umsDhjtProperties.getOrgSenderID());
        head.setOrgReceiverID(umsDhjtProperties.getOrgReceiverID());
        head.setOrgSendDate(DateTimeUtil.getCurrentDay());
        head.setOrgSendTime(DateTimeUtil.getCurrentTime());
        head.setOrgSendTransID(IdWorkerUtils.getInstance().buildPartNumber());

        final UmsSettleBaseRequest<UmsSettleWithdrawalsRequest> settleRequest = new UmsSettleBaseRequest<>();
        settleRequest.setMsgHead(head);
        settleRequest.setMsgBody(settleBaseRequest);

        // 生成签名
        head.setSign(genSign(settleRequest,umsDhjtProperties.getSettleSecret()));
        final String response = platformRequest.extendRequest(umsDhjtProperties.getSettleUrl(), HttpPost.METHOD_NAME, null, settleRequest, false, null);
        final UmsDhjtSettleResponse payResult = JsonUtil.fromJson(response, UmsDhjtSettleResponse.class);
        return payResult;
    }

    /**
     * 发起转账划付请求
     * transType=withdrawals
     *
     * @param umsDhjtProperties 配置
     * @param merId             商户号
     * @return
     */
    public UmsDhjtSettleQueryBalancRes queryBalance(UmsDhjtProperties umsDhjtProperties, String merId) {
        final UmsSettleRequestHead head = new UmsSettleRequestHead();
        head.setOrgSenderID(umsDhjtProperties.getOrgSenderID());
        head.setOrgReceiverID(umsDhjtProperties.getOrgReceiverID());
        head.setOrgSendDate(DateTimeUtil.getCurrentDay());
        head.setOrgSendTime(DateTimeUtil.getCurrentTime());
        head.setOrgSendTransID(IdWorkerUtils.getInstance().buildPartNumber());

        final UmsSettleBaseRequest<UmsSettleWithdrawalsBalanceQueryReq> settleRequest = new UmsSettleBaseRequest<>();
        settleRequest.setMsgHead(head);

        UmsSettleWithdrawalsBalanceQueryReq body = new UmsSettleWithdrawalsBalanceQueryReq();
        body.setMappId(merId);
        body.setTransMid("");
        body.setTransType("getDetail");
        settleRequest.setMsgBody(body);

        // 生成签名
        head.setSign(genSign(settleRequest,umsDhjtProperties.getSettleSecret()));

        final String response = platformRequest.extendRequest(umsDhjtProperties.getSettleUrl(), HttpPost.METHOD_NAME, null, settleRequest, false, null);
        final UmsDhjtSettleQueryBalancRes queryBalancRes = JsonUtil.fromJson(response, UmsDhjtSettleQueryBalancRes.class);
        Asserts.isTrue(Objects.nonNull(queryBalancRes) && queryBalancRes.isOk(), "余额查询失败！");
        return queryBalancRes;
    }

    public UmsDhjtSettleResponse batchQuery(UmsDhjtProperties umsDhjtProperties, UmsSettleWithdrawalsRequest settleBaseRequest) {
        final UmsSettleRequestHead head = new UmsSettleRequestHead();
        head.setOrgSenderID(umsDhjtProperties.getOrgSenderID());
        head.setOrgReceiverID(umsDhjtProperties.getOrgReceiverID());
        head.setOrgSendDate(DateTimeUtil.getCurrentDay());
        head.setOrgSendTime(DateTimeUtil.getCurrentTime());
        head.setOrgSendTransID(IdWorkerUtils.getInstance().buildPartNumber());

        final UmsSettleBaseRequest<UmsSettleWithdrawalsRequest> settleRequest = new UmsSettleBaseRequest<>();
        settleRequest.setMsgHead(head);
        settleRequest.setMsgBody(settleBaseRequest);

        // 生成签名
        head.setSign(genSign(settleRequest,umsDhjtProperties.getSettleSecret()));
        final String response = platformRequest.extendRequest(umsDhjtProperties.getSettleUrl(), HttpPost.METHOD_NAME, null, settleRequest, false, null);
        final UmsDhjtSettleResponse payResult = JsonUtil.fromJson(response, UmsDhjtSettleResponse.class);
        return payResult;
    }

    /**
     * 生成签名字段
     *
     * @param params 参数
     * @param secret 密钥
     * @return
     */
    private String genSign(UmsSettleBaseRequest<? extends UmsSettleRequestBody> params, String secret) {
        Asserts.isNotNull(params, "参数为空！");
        String paramsStr = JsonUtil.toJsonString(params);
        return SecureUtil.sm3X16Str(paramsStr + secret, "UTF-8");
    }
}
